// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 22/03/2011 12:27:40 p.m.
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   AnalisisDeEntradas.java

package com.luxsoft.sw3.impap.ui.consultas;

import ca.odell.glazedlists.*;
import ca.odell.glazedlists.gui.TableFormat;
import com.luxsoft.siipap.cxp.model.CXPAnalisisDet;
import com.luxsoft.siipap.cxp.model.CXPFactura;
import com.luxsoft.siipap.cxp.service.CXPServiceLocator;
import com.luxsoft.siipap.cxp.service.FacturaManager;
import com.luxsoft.siipap.model.Periodo;
import com.luxsoft.siipap.reports.DiarioDeEntradas;
import com.luxsoft.siipap.swing.browser.AbstractMasterDatailFilteredBrowserPanel;
import com.luxsoft.siipap.swing.reports.ReportUtils;
import com.luxsoft.siipap.swing.utils.MessageUtils;
import com.luxsoft.siipap.swing.utils.TaskUtils;
import com.luxsoft.siipap.util.MonedasUtils;
import com.luxsoft.sw3.impap.ui.form.AnalisisDeEntradaForm;
import com.luxsoft.sw3.impap.ui.form.AnalisisDeEntradaModel;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;
import javax.swing.*;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.jdesktop.swingx.JXTable;

public class AnalisisDeEntradas extends AbstractMasterDatailFilteredBrowserPanel
{

    public AnalisisDeEntradas()
    {
        super(com.luxsoft.siipap.cxp.model.CXPFactura.class);
    }

    protected void agregarMasterProperties()
    {
        addProperty(new String[] {
            "id", "fecha", "clave", "nombre", "documento", "fecha", "vencimiento", "moneda", "tc", "total", 
            "totalAnalisis", "bonificado", "importeDescuentoFinanciero", "pagos", "saldoCalculado", "porRequisitar", "requisitado", "comentario"
        });
        addLabels(new String[] {
            "CXP_ID", "Fecha", "Prov", "Nombre", "Docto", "F Docto", "Vto", "Mon", "TC", "Facturado", 
            "Analizado", "Bonificado", "D.F.", "Pagos", "Saldo", "Por Requisitar", "Requisitado", "Comentario"
        });
        installTextComponentMatcherEditor("Proveedor", new String[] {
            "nombre"
        });
        installTextComponentMatcherEditor("Factura", new String[] {
            "documento"
        });
        installTextComponentMatcherEditor("Analisis", new String[] {
            "id"
        });
        manejarPeriodo();
    }

    protected void manejarPeriodo()
    {
        periodo = Periodo.getPeriodoDelMesActual();
    }

    protected TableFormat createDetailTableFormat()
    {
        String props[] = {
            "factura.id", "factura.documento", "entrada.remision", "entrada.fechaRemision", "entrada.fecha", "entrada.documento", "entrada.clave", "entrada.descripcion", "entrada.cantidad", "cantidad", 
            "precio", "costo", "Importe"
        };
        String labels[] = {
            "CXP_ID", "Factura", "Remisi\363n", "F.Remisi\363n", "Entrada", "COM", "Producto", "Descripci\363n", "Recibido", "Analizado", 
            "Precio", "CostoU", "Importe"
        };
        return GlazedLists.tableFormat(CXPAnalisisDet.class, props, labels);
    }

    public Action[] getActions()
    {
        if(actions == null)
            actions = (new Action[] {
                getLoadAction(), getSecuredInsertAction("generarAnalisis"), addAction("generarAnalisis", "generarAnalisisConRecibo", "Nuevo (Contra recibo)"), getDeleteAction(), getEditAction(), addAction("actualizarSeleccion", "actualizarSeleccion", "Actualizar (sel)"), addAction("reporteDeAnalisis", "printReport", "Imprimir"), addAction("diarioDeEntradas", "reporteDiarioDeEntradas", "Diario de entradas")
            });
        return actions;
    }

    protected ca.odell.glazedlists.CollectionList.Model createPartidasModel()
    {
        ca.odell.glazedlists.CollectionList.Model model = new ca.odell.glazedlists.CollectionList.Model() {

            public List getChildren(CXPFactura parent)
            {
                try
                {
                    return getManager().buscarAnalisis(parent);
                }
                catch(Exception e)
                {
                    System.out.println(ExceptionUtils.getRootCauseMessage(e));
                }
                return new ArrayList();
            }

            public List getChildren(Object obj)
            {
                return getChildren((CXPFactura)obj);
            }

            

            
           
        }
;
        return model;
    }

    protected List findData()
    {
        return getManager().buscarFacturas(periodo);
    }

    public void actualizarSeleccion()
    {
        if(!getSelected().isEmpty())
        {
            for(Iterator iterator = getSelected().iterator(); iterator.hasNext();)
            {
                Object row = iterator.next();
                CXPFactura selected = (CXPFactura)row;
                int index = source.indexOf(selected);
                if(index >= 0)
                {
                    CXPFactura fac = (CXPFactura)getManager().get(selected.getId());
                    source.set(index, fac);
                }
            }

        }
    }

    public CXPFactura doInsert()
    {
        CXPFactura factura = new CXPFactura();
        AnalisisDeEntradaModel model = new AnalisisDeEntradaModel(factura);
        AnalisisDeEntradaForm form = new AnalisisDeEntradaForm(model);
        form.open();
        if(!form.hasBeenCanceled())
        {
            CXPFactura target = model.getAnalisis();
            target = (CXPFactura)CXPServiceLocator.getInstance().getFacturasManager().save(target);
            return (CXPFactura)CXPServiceLocator.getInstance().getFacturasManager().get(target.getId());
        } else
        {
            return null;
        }
    }

    protected void afterInsert(CXPFactura bean)
    {
        super.afterInsert(bean);
        print(bean);
    }

    protected CXPFactura doEdit(CXPFactura bean)
    {
        CXPFactura analisis = (CXPFactura)CXPServiceLocator.getInstance().getFacturasManager().get(bean.getId());
        if(analisis.getPagos().doubleValue() != 0.0D)
        {
            MessageUtils.showMessage("El analisis ya tiene pagos aplicados no se puede modificar", "Modificacion de an\341lisis");
            return null;
        }
        if(analisis.getRequisitado().doubleValue() != 0.0D)
        {
            MessageUtils.showMessage("El analisis ya esta requisitado no se puede modificar", "Modificacion de an\341lisis");
            return null;
        }
        AnalisisDeEntradaModel model = new AnalisisDeEntradaModel(analisis);
        AnalisisDeEntradaForm form = new AnalisisDeEntradaForm(model);
        form.open();
        if(!form.hasBeenCanceled())
        {
            CXPFactura target = model.getAnalisis();
            target = (CXPFactura)CXPServiceLocator.getInstance().getFacturasManager().save(target);
            return target;
        } else
        {
            return null;
        }
    }

    public void printReport()
    {
        if(getSelectedObject() != null)
            print((CXPFactura)getSelectedObject());
    }

    public void print(CXPFactura bean)
    {
        if(confirmar("Desea imprimir An\341lisis?"))
        {
            Map params = new HashMap();
            params.put("NUMERO", bean.getId());
            Currency moneda = bean.getMoneda();
            if(!moneda.equals(MonedasUtils.PESOS) && confirmar("En moneda nacional"))
                moneda = MonedasUtils.PESOS;
            params.put("MONEDA", moneda.getCurrencyCode());
            String path = ReportUtils.toReportesPath("cxp/AnalisisDeFactura.jasper");
            if(ReportUtils.existe(path))
                ReportUtils.viewReport(path, params);
            else
                JOptionPane.showMessageDialog(getControl(), MessageFormat.format("El reporte:\n {0} no existe", new Object[] {
                    path
                }), "Reportes", 0);
        }
    }

    public void reporteDiarioDeEntradas()
    {
        DiarioDeEntradas d = new DiarioDeEntradas();
        d.actionPerformed(null);
    }

    protected void executeLoadWorker(SwingWorker worker)
    {
        TaskUtils.executeSwingWorker(worker);
    }

    public boolean doDelete(CXPFactura bean)
    {
        getManager().remove(bean.getId());
        return true;
    }

    private FacturaManager getManager()
    {
        return CXPServiceLocator.getInstance().getFacturasManager();
    }

    protected void adjustMainGrid(JXTable jxtable)
    {
    }

    public Object doEdit(Object obj)
    {
        return doEdit((CXPFactura)obj);
    }

   
    public  boolean doDelete(Object obj)
    {
        return doDelete((CXPFactura)obj);
    }

    public void afterInsert(Object obj)
    {
        afterInsert((CXPFactura)obj);
    }

}
